package com.facebank.usersupport.util;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.UUID;

/**
 * Created by yaozun on 2017/12/6.
 */
public class ImgSaveUtil {
    //添加文件
    public static String uploadImg(MultipartFile file, String path) {
        File pathtest = null;
        try {
            pathtest = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (!pathtest.exists()) pathtest = new File("");
        System.out.println("path:" + pathtest.getAbsolutePath());

//如果上传目录为/static/images/upload/，则可以如下获取：
        File upload = new File(pathtest.getAbsolutePath(), path);
        if (!upload.exists()) upload.mkdirs();
        System.out.println("upload url:" + upload.getAbsolutePath());

        String fileName = file.getOriginalFilename();//获取文件名
        String fileExtensionName = fileName.substring(fileName.lastIndexOf(".") + 1);
        String uploadFileName = UUID.randomUUID().toString() + "." + fileExtensionName;
        System.out.println("开始上传文件，上传文件的文件名：" + fileName + "上传的路径：" + path + "新文件名：" + uploadFileName);
        File targetFile = new File(upload, uploadFileName);
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            System.out.println("上传文件异常" + e);
            return null;
        }
        return targetFile.getName();
    }

    public static String base64Tofile(String imgStr) {
        imgStr = imgStr.substring(22);
        System.out.println("~~~~~~~~~~~~~~~~~~~"+imgStr);
        boolean ss =  generateImage(imgStr, "/images/upload/1234.png");
        System.out.println(ss);
        return "1234.png";
    }


    /**
     * @param imgStr base64编码字符串
     * @param path   图片路径-具体到文件
     * @return
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
// 解密
            byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @return
     * @Description: 根据图片地址转换为base64编码字符串
     * @Author:
     * @CreateTime:
     */
    public static String getImageStr(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


}
